package com.dzsw.netty.tcp;

import com.google.common.base.Objects;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.nio.charset.Charset;
import java.util.Date;

public class TimeServerHandler extends ChannelInboundHandlerAdapter {

    private int count;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("TimeServerHandler => channelInactive");
        ctx.writeAndFlush("连接激活2！");
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("TimeServerHandler => channelReadComplete");
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("TimeServerHandler => exceptionCaught");
        ctx.close();
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("TimeServerHandler => channelRegistered");
        ctx.writeAndFlush("连接成功！");// 客户端收不到
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("TimeServerHandler => channelUnregistered");
        ctx.writeAndFlush("取消注册！");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("TimeServerHandler => channelActive");
        ctx.writeAndFlush("连接激活！");// 客户端收不到
    }

    private void lineBasedFrameHandle(ChannelHandlerContext ctx, String msg){
        String order = msg;
        System.out.println("Server received order:" + order + ";The counter is " + ++count);
        String currentTime = Objects.equal("time", order) ? new Date(System.currentTimeMillis()).toString(): "BAD ORDER";
        currentTime = currentTime + System.getProperty("line.separator");
        ByteBuf buf = Unpooled.copiedBuffer(currentTime.getBytes());
        ctx.writeAndFlush(buf);
    }

    private void byteHandle(ChannelHandlerContext ctx, ByteBuf msg){
        ByteBuf buf = msg;
        byte[] bs = new byte[buf.readableBytes()];
        buf.readBytes(bs);
        String order = new String(bs, Charset.forName("UTF-8"));
        System.out.println("The time server receive order: " + order);
        ByteBuf res = null;
        if (Objects.equal("offline", order)) {
            res = Unpooled.copiedBuffer("离线成功".getBytes());
            ctx.writeAndFlush(res);
            ctx.close();
        } else if (Objects.equal("time", order)) {
            String currentTime = "当前时间：" + (Objects.equal("time", order) ? new Date(System.currentTimeMillis()).toString() : "BAD ORDER");
            res = Unpooled.copiedBuffer(currentTime.getBytes());
            ctx.writeAndFlush(res);
        } else {
            res = Unpooled.copiedBuffer(order.getBytes());
            ctx.writeAndFlush(res);
        }
    }
}
